import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:largefriends/config/lf_route.dart';
import 'package:largefriends/entity/entities/lf_bool_entity.dart';
import 'package:largefriends/net/error_handle.dart';
import 'package:largefriends/net/lf_api_url.dart';
import 'package:largefriends/net/lf_dio_util.dart';
import 'package:largefriends/pages/lf_feedback/lf_feedback_logic.dart';
import 'package:largefriends/res/lf_color.dart';
import 'package:largefriends/res/lf_fonts.dart';
import 'package:largefriends/res/lf_string_ext.dart';
import 'package:largefriends/res/lf_text_style.dart';
import 'package:largefriends/res/strings/lf_strings.dart';
import 'package:largefriends/service/lf_auth_service.dart';
import 'package:largefriends/utils/lf_dialog_utils.dart';
import 'package:largefriends/utils/lf_keyboard.dart';
import 'package:largefriends/widgets/dialog_action_tip.dart';

toForgetPassword({required String email}) {
  Get.toNamed(LFRoute.pathForgetPassword, arguments: {'email': email});
}

class LfForgetPasswordLogic extends GetxController {
  FocusNode focusNode = FocusNode();
  TextEditingController emailController = TextEditingController();
  LfAuthService authService = Get.find();
  RxString email = "".obs;
  RxBool emailFocused = false.obs;
  RxBool canInputEmail = true.obs;

  @override
  void onInit() {
    email.value = Get.arguments['email'] ?? "";
    if (authService.loggedIn() &&
        (authService.currentAccount!.email.isNullOrEmpty == false)) {
      email.value = authService.currentAccount!.email ?? "";
      if (email.value.trim().isEmail) {
        canInputEmail.value = false;
      }
    }

    if (canInputEmail.isTrue) {
      focusNode.addListener(() {
        emailFocused.value = focusNode.hasFocus;
      });
    }
    emailController.text = email.value;
    super.onInit();
  }

  @override
  void onReady() {
    super.onReady();
    if (canInputEmail.value && !email.value.isEmail) {
      showSoftKeyboard(focusNode);
    }
  }

  @override
  void onClose() {
    closeSoftKeyboard();
    emailController.clear();
    focusNode.dispose();
    super.onClose();
  }

  forgetPassword() {
    closeSoftKeyboard();
    showLoading();
    LfDio().post<LfBoolEntity>(
        url: apiForgetPassword, params: {"email": email.value}).then((value) {
      hideLoading();
      showActionTips(
          tips: "",
          tipWidget: Text.rich(TextSpan(
              text: strForgetPasswordEmailTip.tr.split("%%s").first,
              style: lfTextStyle(
                sp: 16.sp,
                fontFamily: fontMedium,
                color: color514F62,
              ),
              children: [
                TextSpan(
                    text: strFeedback.tr,
                    style: lfTextStyle(
                      sp: 16.sp,
                      fontFamily: fontBold,
                      decoration: TextDecoration.underline,
                      color: color514F62,
                    ),
                    recognizer: TapGestureRecognizer()
                      ..onTap = () {
                        hideSheet();
                        Get.back();
                        toFeedback(type: FeedbackType.password);
                      }),
                TextSpan(text: strForgetPasswordEmailTip.tr.split("%%s").last),
              ])),
          actionButtonTip: strOK.tr,
          showCancel: false,
          dismissible: false,
          action: () {
            Get.back();
          });
    }, onError: (err) {
      hideLoading();
      if (err is NetError) {
        showSnackBar(err.msg);
      }
    });
  }
}
